import pandas as pd
import numpy as np
import math


class Population:
    Dic = {}  # 储存所有人口数据的字典

    def staticdata(self, data):  # 标准数据处理
        dic = {}  # 存储json的字典
        column = data[data.columns[0]].values  # 分类
        index = data.columns[1:].values  # 年
        # print(index)

        for i in index:
            item = data[i]
            # print(item)
            count = 0
            li = []  # 存储每个省数据（字典）的列表

            for j in column:
                li.append({j: item.values.tolist()[count]})  # 加入一年各个数据的字典
                count += 1
            dic[int(i[:-1])] = li



        return dic

    def staticprovince(self, data):  # 标准分省数据处理
        dic = {}
        index = data["地区"]
        column = data.columns[1:]
        # print(column)
        for i in index:
            item = data[data["地区"] == i]  # 每个省的数据
            item.index = [i]  # 设置列名
            li = []  # 存储每个省数据（字典）的列表
            # print(item.values[0][1:])
            count = 1
            for j in column:
                li.append({j: item.values[0][count]})
                count += 1
            dic[i] = li
        return dic

    # 年龄

    def age(self):
        age = pd.read_csv("../data/temp/年龄结构.csv", index_col=0, encoding='gbk')
        self.Dic["age"] = self.staticdata(age)

    # 男性
    def gendermale(self):
        gender = pd.read_csv("../data/temp/男生年度数据.csv", index_col=0, encoding='gbk')
        self.Dic["male"] = self.staticdata(gender)

    # 女性
    def genderfemale(self):
        gender = pd.read_csv("../data/temp/女生年度数据.csv", index_col=0, encoding='gbk')
        self.Dic["female"] = self.staticdata(gender)

    # 男女占比
    def rategender(self):
        rategender = pd.read_csv("../data/temp/性别结构.csv", index_col=0, encoding='gbk')
        self.Dic["gender"] = self.staticdata(rategender)

    # 家庭结构
    def family(self):
        # 全国家庭结构
        all_data = {}   # 存放所有数据的字典
        family = pd.read_csv("../data/temp/家庭结构.csv", index_col=0, encoding='gbk')
        divide = family["指标"]
        year = family.columns[1:]
        # print(year)
        china_dic = {}  # 存放全国年数据的字典
        for i in year:
            count = 1
            year_data = []  # 一年的数据
            for j in divide:
                value = family[i][count]
                year_data.append({j: family[i][count]})     # 每个类别的数据
                count += 1
            china_dic[int(i[:-1])] = year_data  # 加入一年的数据
        all_data["china"] = china_dic


        #  分省家庭结构
        one = pd.read_csv("../data/temp/一户家庭结构.csv", index_col=0, encoding='gbk')
        two = pd.read_csv("../data/temp/两户家庭结构.csv", index_col=0, encoding='gbk')
        three = pd.read_csv("../data/temp/三户家庭结构.csv", index_col=0, encoding='gbk')
        more = pd.read_csv("../data/temp/多户家庭结构.csv", index_col=0, encoding='gbk')



        city = one["地区"]    #城市
        for x in city:
            city_dic = {}  # 存放每个省年数据的字典
            for i in year:
                year_data = []  # 一年的数据
                year_data.append({divide.values[0]: one[one["地区"]==x][i].values[0]})  # 每个类别的数据
                year_data.append({divide.values[1]: two[two["地区"]==x][i].values[0]})  # 每个类别的数据
                year_data.append({divide.values[2]: three[three["地区"]==x][i].values[0]})  # 每个类别的数据
                year_data.append({divide.values[3]: more[more["地区"]==x][i].values[0]})  # 每个类别的数据
                # print(year_data)
                city_dic[int(i[:-1])] = year_data  # 加入一年的数据
            all_data[x] = city_dic  # 单个城市所有年份数据加入
        # print(all_data)
        self.Dic["family"] = all_data

    # 新家庭结构
    def family_new(self):
        # 全国家庭结构
        all_data = {}   # 存放所有数据的字典
        family = pd.read_csv("../data/temp/家庭结构_2.csv", index_col=0, encoding='gbk')
        divide = family["指标"]
        year = family.columns[1:]
        # print(year)
        china_dic = {}  # 存放全国年数据的字典
        for i in year:
            count = 1
            year_data = []  # 一年的数据
            for j in divide:
                year_data.append({j: family[i][count]})     # 每个类别的数据
                count += 1
            china_dic[int(i[:-1])] = year_data  # 加入一年的数据
        all_data["china"] = china_dic
        # print(china_dic)

        #  分省家庭结构

        # print(devide.values[5])
        csv_li = []  # 读取的文件数据列表
        for i in divide.values:
            str = i
            if i == "二人户家庭户":   # 处理垃圾不统一数据
                str = "两人户家庭户"
            if i == "十人及以上户家庭户":
                csv_li.append(pd.read_csv("../data/temp/"+"十户及以上家庭结构.csv", index_col=0, encoding='gbk'))
                continue
            # print(str)
            # print("../data/temp/"+str[0]+str[2:-1]+"结构.csv")
            csv_divide = pd.read_csv("../data/temp/"+str[0]+str[2:-1]+"结构.csv", index_col=0, encoding='gbk')
            csv_li.append(csv_divide)
        # print(csv_li)

        city = csv_li[0]["地区"]  # 城市
        for x in city:
            city_dic = {}  # 存放每个省年数据的字典
            for i in year:
                year_data = []  # 一年的数据
                # print(csv_li[1])
                for j in range(10):
                    every_divide = csv_li[j]
                    city_divide = every_divide[every_divide["地区"] == x][i].values[0]
                    if(math.isnan(city_divide)):
                        city_divide = 0
                    year_data.append({divide.values[j]:city_divide})
                city_dic[int(i[:-1])] = year_data  # 加入一年的数据
            all_data[x] = city_dic  # 单个城市所有年份数据加入
            # print(x)
        # print(all_data)
        self.Dic["family_new"] = all_data
        # print(all_data["北京市"])



        # city = one["地区"]  # 城市
        # for x in city:
        #     city_dic = {}  # 存放每个省年数据的字典
        #     for i in year:
        #         year_data = []  # 一年的数据
        #         year_data.append({devide.values[0]: one[one["地区"] == x][i].values[0]})  # 每个类别的数据
        #         year_data.append({devide.values[1]: two[two["地区"] == x][i].values[0]})  # 每个类别的数据
        #         year_data.append({devide.values[2]: three[three["地区"] == x][i].values[0]})  # 每个类别的数据
        #         year_data.append({devide.values[3]: more[more["地区"] == x][i].values[0]})  # 每个类别的数据
        #         # print(year_data)
        #         city_dic[int(i[:-1])] = year_data  # 加入一年的数据
        #     all_data[x] = city_dic  # 单个城市所有年份数据加入
        # # print(all_data)
        # self.Dic["family"] = all_data


    # 变化数据

    def change(self):
        # 分省变化数据
        birth = pd.read_csv("../data/temp/分省出生率数据.csv", index_col=0, encoding='gbk')
        die = pd.read_csv("../data/temp/分省死亡率数据.csv", index_col=0, encoding='gbk')
        ascent = pd.read_csv("../data/temp/分省增长率数据.csv", index_col=0, encoding='gbk')
        # self.Dic["birth"] = self.staticprovince(birth)
        # self.Dic["die"] = self.staticprovince(die)
        # self.Dic["ascent"] = self.staticprovince(ascent)
        data = {}
        columns = birth.columns[1:]
        for i in birth["地区"]:
            item1 = list(np.array(birth[birth["地区"] == i])[0][1:])
            item2 = list(np.array(die[birth["地区"] == i])[0][1:])
            item3 = list(np.array(ascent[birth["地区"] == i])[0][1:])
            # print(item1)
            li = []  # 单个城市所有年的数据
            count = 0
            for j in columns:
                if math.isnan(item1[count]):
                    continue
                li.append({j: [item1[count], item2[count], item3[count]]})
                count += 1
            data[i] = li
        # print(data)

        # 全国变化数据

        change = pd.read_csv("../data/temp/年度变化数据.csv", index_col=0, encoding='gbk')
        dataAll = []
        for i in change.columns[1:]:
            item = list(np.array(change[i]))
            dic = {}
            dic[i] = item
            dataAll.append(dic)
        data['china'] = dataAll

        self.Dic["change"] = data


    # 全国总人口
    def population(self):
        population = pd.read_csv("../data/temp/年度数据.csv", index_col=0, encoding='gbk')
        data = population[population["指标"] == "年末总人口(万人)"].drop("指标", axis=1)
        columns = data.columns
        li = []  # 存储列表
        count = 0
        # print(columns.size)
        # print(data.values)
        for i in columns:
            li.append({i:data.values.tolist()[0][count]})
            count +=1
        # print(li)
        self.Dic["population"] = li

    # 2023年各个省人口
    def eachpopulation(self):
        population = pd.read_csv("../data/temp/分省常驻人口数据.csv", index_col=0, encoding='gbk')
        index = population["地区"]
        data = population["2023年"]
        li = []     #存放各个省人口的列表
        # print(data)
        for i in range(index.size):
            li.append({index[i]:data[i]})
        self.Dic["lastyear"] = li   # 2023年各个省的人口



    def getdata(self):  # Dic获取数据
        self.age()
        self.family()
        self.family_new()
        self.genderfemale()
        self.gendermale()
        self.change()
        self.population()
        self.rategender()
        self.eachpopulation()


# 测试
# p = Population()
# p.getdata()
# print(p.Dic["gender"][2023])
# print(p.Dic["family_new"]["内蒙古自治区"][2014])
# print(p.Dic["lastyear"])
# print(p.Dic["change"]["北京市"])
# print(p.Dic["change"])
# print(p.Dic["male"][2021])
# print(p.Dic["male"][2018])
# print(p.Dic["age"])
